﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.PurchaseOrder;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.PurchaseOrder
{
    public class OrderPoServices : IOrderPoServices
    {
        public OrderPoServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }
        public async Task<IEnumerable<OrderPo>> GetAllAsync()
        {
            string sql = "select * from OrderPo";
            var list = await Db.QueryAllAsync<OrderPo>(sql);
            return list;

        }

        public async Task<IEnumerable<OrderPo>> GetAsync(int PageIndex, int PageSize, string Number, string VendorId, string WarehouseId, int Status)
        {
            string sql = "select * from OrderPo where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql +=  " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId ";
            }
            if (Status!=-1)
            {
                sql += " and Status =@Status";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<OrderPo>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", VendorId = $"%{VendorId}%", WarehouseId = $"%{WarehouseId}%", Status });
            return list;
        }

        public async Task<int> GetCountAsync(string Number, string VendorId, string WarehouseId, int Status)
        {
            string sql = "select count(*) from OrderPo where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(VendorId))
            {
                sql += " and VendorId like @VendorId ";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId";
            }
            if (Status != -1)
            {
                sql += " and Status =@Status";
            }

            var list = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", VendorId = $"%{VendorId}%", WarehouseId = $"%{WarehouseId}%", Status });
            return list;
        }

        public async Task<IEnumerable<OrderPoItemDto>> GetOrderPoIdAsync(string OrderPoId,string WarehouseId)
        {
            var list = await Db.QueryAsync<OrderPoItemDto>(
                @"select item.Spec,item.ItemName,item.ItemCode,item.UnitOfMeasure,item.Model,item.Remark,op.*,(isk.StockCount-isk.SalesCount) as CurrCount
                 from OrderPoItem op inner join Item item on op.ItemId=item.Id
                 inner join ItemStock isk on isk.ItemId=item.Id and isk.WareHouseId=@WareHouseId
                 where op.OrderPoId = @OrderPoId", new { OrderPoId, WarehouseId });
            return list;
        }

        public async Task<bool> UpdateAsync(OrderPo model, List<OrderPoItem> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model, list);
            return result;
        }

        public async Task<bool> AddAsync(OrderPo model, List<OrderPoItem> list)
        {
            var result = await Db.InsertIdentityTranAsync(model, list);
            return result;

        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete OrderPo where Id in @Ids;delete OrderPoItem where OrderPoId in @Ids;";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete OrderPo where Id=@Id;delete OrderPoItem where OrderPoId = @Id;";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }
        public async Task<bool> UpdateOrderPoStatus(long Id, int Status)
        {
            string sql = $"update OrderPo set Status=@Status where Id =@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }
    }
}
